<template>
  <div class="container">
    <el-row class="table-top">
      <div class="left">
        <el-input v-model="name" placeholder="请输入部门名称" clearable @input="getDepartmentList" />
      </div>
      <div class="right">
        <el-button v-permission="['81002']" type="primary" @click="openFrom"> 新建部门 </el-button>
      </div>
    </el-row>

    <el-table v-permission="['81001']" :data="tableData" style="width: 100%" :header-cell-style="{background:'#F7F8FA'}">
      <el-table-column prop="department_name" align="center" label="部门名称" />
      <el-table-column prop="leader.name" align="center" label="部门负责任人" />
      <el-table-column align="center" label="操作" width="120">
        <template slot-scope="scope">
          <el-button v-permission="['81003']" type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button v-permission="['81004']" type="text" size="small" @click="handleDeleta(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <DepartmentFrom :id="editId" ref="DepartmentFrom" :visible="visible" :form="editFrom" @close="closeFum" @success="successFun" />
  </div>
</template>

<script>
import DepartmentFrom from './components/DepartmentFrom'
import {
  getDepartmentList } from '@/api/employee'
import { deleteDepartment } from '@/api/base'
export default {
  name: 'BaseDepartment',
  components: {
    DepartmentFrom
  },
  data() {
    return {
      name: '',
      tableData: [],
      visible: false,
      editId: null,
      editFrom: {}
    }
  },
  mounted() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await getDepartmentList(this.name)
      if (res.ret === 0) {
        this.tableData = res.data
      }
    },
    async handleDeleta(row) {
      const isDelete = await this.$confirm(`确定删除 ${row.department_name} 部门吗？`, '提示', { type: 'warning' })
      if (!isDelete) {
        return
      }
      const res = await deleteDepartment(row.ID)
      if (res.ret === 0) {
        this.$message.success('删除成功')
        this.getDepartmentList()
      }
    },
    handleEdit(row) {
      this.editId = row.ID
      this.editFrom = JSON.parse(JSON.stringify(row))
      this.visible = true
    },
    openFrom() {
      this.editId = 0
      this.visible = true
    },
    closeFum() {
      this.visible = false
    },
    successFun() {
      this.visible = false
      this.getDepartmentList()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
